// pages/gerenZiliao/gerenZiliao.js
import WxValidate from '../../utils/WxValidate.js';
const app = getApp()
Page({

  /**
   * 页面的初始数据
   */
  data: {
    sex: ['男', '女'],//性别列表
    sexindex: 0,//选择的性别下标
    positionindex: 4,//选择的 学历的下标
    position: ['初中', '高中', '中专', '大专', '本科', '硕士', '博士'],//学历数组
    date: '2001-01-01',//选择的时间（默认）
    headimg: '',
    region: ['湖北省', '荆州市', '江陵县'],
    customItem: '全部',
    fun_id: 2,
    time: '获取验证码', //倒计时 
    currentTime: 61
  },
  // citypicker
  bindRegionChange: function (e) {
    console.log('picker发送选择改变，携带值为', e.detail.value)
    this.setData({
      region: e.detail.value
    })
  },
  getCode: function (options) {
    var that = this;
    var currentTime = that.data.currentTime;

    var interval = setInterval(function () {
      currentTime--;
      that.setData({
        time: currentTime + '秒'
      })
      if (currentTime <= 0) {
        clearInterval(interval)
        that.setData({
          time: '重新发送',
          currentTime: 61,
          disabled: false
        })
      }
    }, 1000)
  },
  getVerificationCode:function(e){
    this.getCode();
    var that = this
    that.setData({
      disabled: true
    })
  },
  
  //选择性别
  bindsexChange: function (e) {
    console.log(e.detail.value)
    var index = e.detail.value;
    this.setData({
      sexindex: index
    })
  },
  // 选择 学历
  bindpositionChange: function (e) {
    var positionindex = e.detail.value;
    this.setData({
      positionindex: positionindex
    })
  },
  //选择入党时间
  bindtimeChange: function (e) {
    this.setData({
      date: e.detail.value
    })
  },
  //报错 
  showModal(error) {
    wx.showModal({
      content: error.msg,
      showCancel: false,
    })
  },

  //验证函数
  initValidate() {
    const rules = {
      nickname: {
        required: true,
        // minlength: 2
      },
      phone: {
        required: true,
        tel: true
      },
      code: {
        required: true,
      },
      password:{
        required: true,
        minlength: 6,
        maxlength: 12,
      }, 
      password_2: {
        required: true,
      },
      id_num:{
        required: true,
        idcard: true
      },
      address: {
        required: true
      }
    }
    const messages = {
      nickname: {
        required: '请填写姓名',
        minlength: '请输入正确的名称'
      },
      phone: {
        required: '请填写手机号',
        tel: '请填写正确的手机号'
      },
      code: {
        required: '请填写验证码',
      },
      password: {
        required: '请输入密码',
        minlength: '密码长度不能小于6位',
        maxlength: '密码长度不能大于12位',
      },
      password_2: {
        required: '请确认密码',
      },
      id_num: {
        required: "请输入身份证号码",
        idcard: "请输入正确的身份证号码"
      },
      address: {
        required: '请输入详细地址'
      }
    }
    this.WxValidate = new WxValidate(rules, messages);
  },
  formSubmit:function(e){
    console.log('form发生了submit事件，携带数据为：', e.detail.value);
    var _this = this;
    var params = e.detail.value;
    params.user_name = params.phone;
    //校验表单
    if (!this.WxValidate.checkForm(params)) {
      const error = this.WxValidate.errorList[0]
      this.showModal(error)
      return false
    }else{
      params.password !== params.password_2 ? this.showModal({msg: '两次输入密码不一致!'}):
      wx.request({
          url: app.globalData.url+'/api/register',
          data: params,
          method:'POST',
          success:function(res){
            console.log(res)
            if(res.data.status != 'success'){
              _this.showModal({ msg: '注册失败' })
            }

            wx.navigateBack()
            
          }
      })
      
    };   
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    this.initValidate()
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})